﻿#region Directives

using ConnectIntegrationHub.WebService.Common;
using Microsoft.Xrm.Sdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Services;

#endregion Directives

namespace ConnectIntegrationHub.WebService
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class WebService : System.Web.Services.WebService
    {
        #region Properties
        
        public Service Service
        {
            get;
            set;
        }
        
        #endregion Properties

        #region Constructors

        public WebService()
        {
            this.Service = new Service(false);
        }

        #endregion Constructors

        #region Public Methods

        [WebMethod(true)]
        public void processDeploymentSynchronization(String deploymentSynchronizationId)
        {
            if (!deploymentSynchronizationId.IsNullOrEmpty())
            {
                this.Service.processDeploymentSynchronization(new Guid(deploymentSynchronizationId));
            }            
        }

        [WebMethod(true)]
        public List<Entity> getEntityData(String entityName, String fieldNames, Int32 recordCount, Boolean includeStateCode)
        {
            List<Entity> result = new List<Entity>();
            Int32? TotalRecordCount = recordCount == 0 ? null as Int32? : recordCount;

            if (!fieldNames.IsNullOrEmpty())
            {
                result = this.Service.getEntityData(entityName, fieldNames.Split(Common.Strings.specialCharacter_Comma).ToList(), null, TotalRecordCount, includeStateCode);
            }
            else
            {
                result = this.Service.getEntityData(entityName, null, null, TotalRecordCount, includeStateCode);
            }
            return result;
        }

        #endregion Public Methods        
    }
}